import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import ItemEdit from '../views/ItemEdit.vue'
import CategoryEdit from '../views/CategoryEdit.vue'
import HeroEdit from '../views/HeroEdit.vue'
import ArticleEdit from '../views/ArticleEdit.vue'
import AdEdit from '../views/AdEdit.vue'
import AdminUserEdit from '../views/AdminUserEdit.vue'

Vue.use(VueRouter)

  const routes = [
    {
      path:'/login',
      name:'login',
      component: ()=> import('../views/Login.vue'),
      meta:{ isPublic:true }
    },
    {
      path: '/',
      name: 'main',
      component: Main ,
      children: [
        {  path: 'categories/create', component: CategoryEdit   },
        {  path: 'categories/edit/:id', component: CategoryEdit, props: true },
        {  path: 'categories/list', component: ()=> import('../views/CategoryList.vue') },

        {  path: 'items/create',component: ItemEdit  },
        {  path: 'items/edit/:id', component: ItemEdit, props: true },
        {  path: 'items/list',  component: ()=> import('../views/ItemList.vue') },

        {  path: 'heroes/create',component: HeroEdit  },
        {  path: 'heroes/edit/:id', component: HeroEdit, props: true },
        {  path: 'heroes/list',  component: ()=> import('../views/HeroList.vue') },

        {  path: 'articles/create',component:  ArticleEdit  },
        {  path: 'articles/edit/:id', component:  ArticleEdit, props: true },
        {  path: 'articles/list',  component: ()=> import('../views/ArticleList.vue') },

        {  path: 'ads/create',component:  AdEdit  },
        {  path: 'ads/edit/:id', component:  AdEdit, props: true },
        {  path: 'ads/list',  component: ()=> import('../views/AdList.vue') },

        {  path: 'admin_users/create',component:  AdminUserEdit  },
        {  path: 'admin_users/edit/:id', component:  AdminUserEdit, props: true },
        {  path: 'admin_users/list',  component: ()=> import('../views/AdminUserList.vue') }
      ]
    }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to ,from ,next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})

export default router
